<?php
namespace app\index\model;

use app\common\model\RemoteModel;
use think\Db;
use think\Model;

class Devretention extends RemoteModel
{

    protected $connection = 'db_config_dragon';

    /**
     * 初始化
     *
     * @return   void
     */
    protected function initialize()
    {
        parent::initialize();
        $this->table = 'devretention';
    }

    /**
     * 列表(主键列表，条件数组，闭包查询)
     *
     * @param    array $param 查询条件
     * @return   void
     */
    public function doList($param = null)
    {

        try {

            //查询数据
            $result = self::all($param);
            $rows   = [];

            foreach ($result as $data) {
                $item = $data->getData();
                array_push($rows, $item);
            }

            //返回结果
            $res = array();

            $total = count($rows);

            if ($total > 0) {
                $res['rows']  = $rows;
                $res['total'] = $total;
            } else {
                $res['total'] = 0;
                $res['rows']  = array();
            }

            //请求类型返回
            return ($res);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }

    /**
     * 返回数据表主键ID最大值
     *
     * @return int 当前id最大值
     */
    public function getRecentDate()
    {
        try {

            $sql = "select date from `devretention` order by `date` desc limit 1";
            $Db  = Db::connect($this->connection);
            $res = $Db->query($sql);

            return $res ? $res[0]['date'] : null;

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }

}
